﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ecommerce.Model
{
    public class EnderecoModel : BaseData
    {
        private static EnderecoModel instance;

        private EnderecoModel()
        {

        }

        public static EnderecoModel Instance
        {
            get
            {
                if (instance == null)
                    instance = new EnderecoModel();
                
                return instance;
            }
        }

        public List<tb_end_endereco> ListarPorPessoa(int identificadorPessoa)
        {
            List<tb_end_endereco> lista;
            using (EcommerceEntities db = new EcommerceEntities(this.InicializarStringConexao(), false))
            {
                lista = db.tb_end_endereco.Where(x => x.pes_idt_pessoa.Equals(identificadorPessoa)).OrderBy(x => x.end_nom_logradouro).ToList();
            }
            return lista;
        }

        public List<tb_ten_tipo_endereco> ListarTipoEndereco()
        {
            using (EcommerceEntities db = new EcommerceEntities(this.InicializarStringConexao(), false))
                return db.tb_ten_tipo_endereco.ToList();
        }

        public tb_end_endereco Obter(int identificadorEndereco)
        {
            tb_end_endereco item;
            using (EcommerceEntities db = new EcommerceEntities(this.InicializarStringConexao(), false))
            {
                item = db.tb_end_endereco.Where(x => x.end_idt_endereco.Equals(identificadorEndereco)).FirstOrDefault();
            }
            return item;
        }

        public void Inserir(tb_end_endereco endereco)
        {
            try
            {
                using (EcommerceEntities db = new EcommerceEntities(this.InicializarStringConexao(), false))
                {
                    db.tb_end_endereco.Add(endereco);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

    }
}
